const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: 0,
    formdata:'',
    modalName: null,
    isName:"",
    isTel:"",
    isWechat:"",
    searchvalue:'',
    picker: [],
    selectPro:'',
    ids: [],
    selectProId:'',
    newhouse_project_id:'',
  },
	onLoad(){
    
	},
  onShow() {
    let open_id = wx.getStorageSync('user_new')
    app.core('get', 'getUserInfo?open_id=' + open_id, '', (res) => {
      console.log(res.data.adviser_id)
      let isadviser = res.data.adviser_id
      if (isadviser !== 0) {
        wx.showModal({
          title: '提示',
          content: '您已注册成为置业顾问，请勿重复注册',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/member/index',
              })
            } else if (res.cancel) {
              wx.reLaunch({
                url: '/pages/member/index',
              })
            }
          }
        })
      }
    })
  },
  // 姓名
  nameVal(e){
    this.setData({
      isName: e.detail.value
    })
  },
  // 手机
  telVal(e) {
    this.setData({
      isTel: e.detail.value
    })
  },
  // 微信
  wechatVal(e) {
    this.setData({
      isWechat: e.detail.value
    })
  },
  // 楼盘
  searchlist(e) {
    this.setData({
      searchvalue: e.detail.value
    })
  },
  // PickerChange
  // PickerChange(e) {
  //   let picker = this.data.picker
  //   let proId = this.data.proId
  //   console.log(picker[e.detail.value])
  //   this.setData({
  //     index: e.detail.value,
  //     selectPro: picker[e.detail.value],
  //     selectProId: proId[e.detail.value]
  //   })
  // },
  //搜索框内容
  searchdesc() {
    var that = this;
    var setid = app.globalData.cityCode;
    var searchvalue = that.data.searchvalue;
    
    if (searchvalue == '') {
      wx.showToast({
        title: '请输入内容',
        duration: 1000,
        icon: 'none'
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      app.core('get', 'getNewHouseProjectBySiteId?siteId=' + setid + '&projectName='+searchvalue,'',           (res) => {
        console.log(res.data)
        // let proName = []
        // let proId = []
        // for (let i in res.data) {
          
        //   proName.push(res.data[i].projectName)
        //   proId.push(res.data[i].id)
        // }
        // console.log(proName)
        // console.log(proId)
        // this.setData({
        //   picker: proName,
        //   proId: proId
        // })
        this.setData({ picker: res.data })
        if (res.data == '') {
          wx.showToast({
            title: '暂无相关信息,请输入正确楼盘，支持模糊搜索',
            duration: 2000,
            icon: 'none'
          })
          return false
        } 
        wx.hideLoading()
      })
    }

  },
  selectitem(e) {
    let ids = e.detail.value
console.log(ids,111)
    this.setData({
      ids: ids
    })
  },
  /*
   if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){
        alert("手机号码有误，请重填");
        return false;
    }
  */
  //提交注册
  Registerpost () {
    var openid = wx.getStorageSync('user_new');
    var vm = this;
    var isTel = vm.data.isTel;
    var isName = vm.data.isName;
    // let index = vm.data.index
    // let proId = vm.data.proId
    // let newhouse_project_id = proId[index]

    var idsArr = vm.data.ids;
    if (idsArr.length == 0) {
      wx.showToast({
        title: '请绑定楼盘',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    var newhouse_project_id = idsArr.join(',')

    if (vm.data.isName == '' || isTel == '' || vm.data.isWechat == ''){
      wx.showToast({
        title: '带 * 为必填',
        icon: 'none',
        duration: 2000
      });
      return false;
    } 
    else if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(isName))) {
      wx.showToast({
        title: '姓名格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(isTel))) {
      wx.showToast({
        title: '手机号码格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else if (newhouse_project_id == undefined) {
      wx.showToast({
        title: '请搜索后绑定楼盘',
        icon: 'none',
        duration: 2000
      });
      return false;
    } 
    else{ 
      let  name = vm.data.isName
      let  phone = vm.data.isTel
      let  wechatId = vm.data.isWechat
      let  siteId = app.globalData.cityCode
      let  openid = wx.getStorageSync('user_new')
      let  useInfo = wx.getStorageSync('userInfo')
      let  headpic = useInfo.avatarUrl

      var idsArr = vm.data.ids;
      var newhouse_project_id = idsArr.join(',')
      console.log(idsArr, 2221)
      console.log(newhouse_project_id, 333)
      
      // let  newhouse_project_id = vm.data.selectProId
      // let index = vm.data.index
      // let proId = vm.data.proId
      // let newhouse_project_id = proId[index]
      // console.log(index,proId,newhouse_project_id)
      app.core('get', 'registerAdviser', {
        'name':name,
        'phone': phone,
        'headpic': headpic,
        'wechatId': wechatId,
        'siteId': siteId,
        'open_id': openid,
        'newhouse_project_id': newhouse_project_id
      }, (res) => {
        console.log(res);
        if (res.resultCode == 200){
          wx.showToast({
            title: '注册成功',
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/member/index',
            });
          })
        }else{
          let msg = res.msg
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
        (err) => {
          console.log(err)
        }
      )

    }
    
  }
})